﻿/**********************************************************
 ●项目：Smartkernel
 ●公司：智能核心信息技术研发中心
 ●作者：曹艳白
 ●邮箱：caoyanbai@gmail.com
 ●创建：2008-08-10
**********************************************************/
using System;

namespace Smartkernel
{
    /// <summary>
    /// 中国货币处理
    /// </summary>
    public static class SmartChineseCurrency
    {
        /// <summary>
        /// 将金额从阿拉伯数字转换为中文大写形式
        /// </summary>
        /// <param name="money">金额</param>
        /// <returns>中文大写形式</returns>
        public static string ConvertTo(object money)
        {
            decimal decimalMoney = SmartReflection.ConvertTo<decimal>(money.ToString());
            string stringMoney = decimalMoney.ToString("#.##");
            if (stringMoney.Length == 0)
            {
                stringMoney = "0";
            }
            string output = string.Empty;
            string[] scales = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟" };
            string[] bases = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

            string part1 = stringMoney.ToString();
            string part2 = string.Empty;
            //计算小数点前面的部分
            if (stringMoney.IndexOf(".") > -1)
            {
                part1 = stringMoney.Substring(0, stringMoney.IndexOf("."));
                part2 = stringMoney.Substring(stringMoney.IndexOf(".") + 1, stringMoney.Length - stringMoney.IndexOf(".") - 1);
            }
            for (int i = part1.Length; i > 0; i--)
            {
                int count = Convert.ToInt16(part1[part1.Length - i]);
                output += bases[count - 48];
                output += scales[i + 1];
            }
            //计算小数点后面的部分
            for (int i = 0; i < part2.Length; i++)
            {
                int count = Convert.ToInt16(part2[i]);
                output += bases[count - 48];
                if (i == 0)
                {
                    output += "角";
                }
                if (i == 1)
                {
                    output += "分";
                }
            }
            return output;
        }
    }
}
